<?php
namespace app\admin\model\myshuju\bind;
use app\admin\builder\Builder;
use app\admin\model\myshuju\myconfigs\MyConFig;
use think\facade\Db;
use think\facade\View;

class BuliderToTable extends MyConFig
{
    public $field = [];
    public $paichu = [];
    public $toops ='';
    public $button = '';
    public $page = [];
    public $rightheader = [
        'desc'=>'操作',
        'width'=>200
    ];
    public $builder = '';
    public function __construct()
    {
        $this->builder = Builder::table();
        return $this;
    }
    public function setPage($str='',$isopen=true){
        $this->page['str'] = $str;
        $this->page['isopen'] = $isopen;
        return $this;
    }
    public function setTableToops($toops=''){
        $this->toops = $toops;
        return $this;
    }
    protected function getToops(){
        if(!$this->toops){
            if($this->cid){
                $this->toops = $this->cidinfo['toops'];
            }else{
                if($this->model_id){
                    $this->toops = $this->model['toops'];
                }
            }
        }
        return $this->toops;
    }
    public function RightHeader($desc='',$width=200){
        $this->rightheader['desc'] = $desc;
        $this->rightheader['width'] = $width;
        return $this;
    }
    public function setButton($button=''){
        $this->button = $button;
        return $this;
    }
    protected function getButton(){
        if(!$this->button){
            if($this->cid){
                $this->button = $this->cidinfo['rigth'];
            }else{
                if($this->model_id){
                    $this->button = $this->model['right'];
                }
            }
        }
        return $this->button;
    }
    public function fetch($table='table',$callback=''){
        if(!$this->page['str']){
            $this->page['str'] = request()->controller().'/page';
            if($this->cid){
                $this->page['str'] = $this->page['str'].'?cid='.$this->cid;
            }else{
                if($this->model_id){
                    $this->page['str'] = $this->page['str'].'?model_id='.$this->model_id;
                }
            }
        }
        if(!$this->button){
            $this->button = $this->getButton();
        }
        $this->builder = $this->builder->setAction($this->page['str'])
            ->setPage($this->page['isopen']);
        $toops = explode(',',$this->getToops());
//        echo '<pre>';
//        print_r($toops);
//        exit;
        $fieldsousuo = [];
        $timesousuo = [];
        foreach ($this->field as $value){
            if($value['search']){
                if($value['form_type']=='时间'){
                    $timesousuo[] = $value;
                }else{
                    if($value['form_type']=='联动单选框'||$value['form_type']=='联动下拉框'){
                        $data = Db::name(getTable('models'))->where('id',$value['table'])->find();
                        if($data){
                            if($value['option']){
                                $op = ','.$value['option'];
                            }else{
                                $op = ',name';
                            }
                            $data = Db::name($data['table'])->where('status',1)->field('id'.$op)->select()->toArray();
                            $value['data'] = $data;
                        }else{
                            $value['data'] = [];
                        }
                    }elseif($value['form_type']=='单选框'){
                        $data = explode(',',$value['option']);
                        $newdata = [];
                        foreach ($data as $key=>$val){
                            $newdata[$key]['id'] = $val;
                            $newdata[$key]['name'] = $val;
                        }
                        $value['data'] = $newdata;
                    }else{
                        $value['data'] = [];
                    }
                    $fieldsousuo[] = $value;
                }
            }
        }
        if($this->cid){
            $this->builder = $this->builder->setCid($this->cid);
            $this->builder = $this->builder->setMonitorAction('switch',request()->controller().'/change?cid='.$this->cid);
        }else{
            if($this->model_id){
                $this->builder = $this->builder->setModelId($this->model_id);
                $this->builder = $this->builder->setMonitorAction('switch',request()->controller().'/change?model_id='.$this->model_id);
            }
        }
        $this->builder = $this->builder
//            ->setMonitorAction('switch',request()->controller().'/change')
//            ->setMonitorAction('edit',request()->controller().'/change')
            ->setRightHeader($this->rightheader['desc'],$this->rightheader['width']);
        foreach ($toops as $value){
            if($value=='alldel'){
                $this->builder = $this->builder->addToolbarDemo('alldel','全部删除',[],request()->controller().'/del');
            }
            if($value=='add'){
                $this->builder = $this->builder->addToolbarDemo('add','添加',[],request()->controller().'/show_add');
            }
            if($value=='back'){
                $this->builder = $this->builder->addToolbarDemo('back','返回');
            }
            if($value=='text'){
                $this->builder = $this->builder->addToolbarDemo('text',$this->table);
            }
            if($value=='export'){
                if($this->cid){
                    $this->builder = $this->builder->addToolbarDemo('export','导出',[],request()->controller().'/exports?cid='.$this->cid);
                }else{
                    $this->builder = $this->builder->addToolbarDemo('export','导出',[],request()->controller().'/exports?model_id='.$this->model_id);
                }
            }
        }
//        echo '<pre>';
//        print_r($fieldsousuo);
//        exit;
        if($fieldsousuo){
            $this->builder = $this->builder->addToolbarDemo('search','搜索',$fieldsousuo);
        }
        if($timesousuo){
            $this->builder = $this->builder->addToolbarDemo('times','确定',$timesousuo);
        }
        $right = explode(',',$this->button);
        if(count($right)>3){
            $this->builder->setRightHeader(null,300);
        }
        foreach ($right as $value){
            if($value=='edit'){
                $this->builder = $this->builder->setButton($value,'编辑',request()->controller().'/show_edit');
            }
            if($value=='del'){
                $this->builder = $this->builder->setButton($value,'删除',request()->controller().'/del');
            }
            if($value=='add'){
                $this->builder = $this->builder->setButton($value,'添加',request()->controller().'/show_add');
            }
            if($value=='detail'){
                $this->builder = $this->builder->setButton($value,'查看',request()->controller().'/detail');
            }
            if($value=='a'){
                if($this->cid){
                    $zhege = $this->cidinfo;
                }else{
                    $zhege = $this->model;
                }
                $zhege['a_action'] = explode(',',$zhege['a_action']);
                $zhege['a_desc'] = explode(',',$zhege['a_desc']);
                foreach ($zhege['a_action'] as $s=>$aa){
                    $this->builder = $this->builder->setButton($value,$zhege['a_desc'][$s]??'跳转',$aa);
                }
            }
            if($value=='button'){
                if($this->cid){
                    $zhege = $this->cidinfo;
                }else{
                    $zhege = $this->model;
                }
                $zhege['button_action'] = explode(',',$zhege['button_action']);
                $zhege['button_desc'] = explode(',',$zhege['button_desc']);
                foreach ($zhege['button_action'] as $s=>$aa){
                    $this->builder = $this->builder->setButton($value,$zhege['button_desc'][$s]??'点击',$aa);
                }
            }
        }
        foreach ($this->field as $value){
            if($value['show']){
                if($value['field']=='name'&&!$this->cid){
                    View::assign('woqu',1);
                    $this->builder = $this->builder->setColumn('pid',$value['field'],$value['field_name'],'lianbiao/change?model_id='.$this->model_id,null,'left');
                    continue;
                }
                if($value['field']=='pid'){
                    $this->builder =  $this->builder->setColumn('input','pidname',$value['field_name'],'document/change');
                    continue;
                }
                if($value['form_type']=='无限联动框'||$value['form_type']=='单行文本'||$value['form_type']=='多行文本'||$value['form_type']=='下拉框'||$value['form_type']=='联动下拉框'||$value['form_type']=='编辑框'||$value['form_type']=='编辑框(layui)'||$value['form_type']=='单选框'||$value['form_type']=='多选框'||$value['form_type']=='联动单选框'||$value['form_type']=='联动多选框'||$value['form_type']=='联动下拉组'||$value['form_type']=='联动单行文本'){
                    $totalRow = false;
                    if($value['he']==1){
                        $totalRow =true;
                    }
                    $this->builder =  $this->builder->setColumn('input',$value['field'],$value['field_name'],'document/change','','center','','',$totalRow);
                }
                if($value['form_type']=='时间'){
                    $this->builder =  $this->builder->setColumn('input',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='单图片'){
                    $this->builder =  $this->builder->setColumn('img',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='多图片'){
                    $this->builder =  $this->builder->setColumn('imgs',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='开关'){
                    if($value['option']){
                        $array = explode(',',$value['option']);
                    }else{
                        $array = ['开启','关闭'];
                    }
                    $this->builder =  $this->builder->setColumn('switch',$value['field'],$value['field_name'],'document/change',null,'center','',$array);
                }
                if($value['form_type']=='json数组'){
                    $this->builder =  $this->builder->setColumn('input',$value['field'],$value['field_name']);
                }
            }
        }
        if($callback){
            $this->builder = $callback($this->builder,$this->field);
        }
        return $this->builder->fetch($table);
    }
}